<?
class Thanh_vien_model extends Model{
	
	var $_username;
	var $_password;
	var $_fullname;
	var $_email;
	var $_phone;
	var $_address;
	var $_website;	
	var $_message;
	
	var $_editting;
	var $_tab_id = 1;
	
	function Thanh_vien_model(){
		parent::Model();		
		$this->_editting = false;
	}
	
	function dang_ky_tai_khoan(){
		if(FALSE!==$this->input->post('hidden_status')){
			$this->_username 	= $this->input->post('txt_username');
			$this->_password 	= $this->input->post('txt_password');
			$this->_fullname 	= $this->input->post('txt_fullname');
			$this->_email 		= $this->input->post('txt_email');
			$this->_phone 		= $this->input->post('txt_phone');
			$this->_address 	= $this->input->post('txt_address');
			$this->_website 	= $this->input->post('txt_website');
			// validate captcha			
			$res = recaptcha_check_answer($this->config->item('recaptcha_private_key'),
											$_SERVER["REMOTE_ADDR"],
											$this->input->post('recaptcha_challenge_field'),
											$this->input->post('recaptcha_response_field'));			
			if(!$res->is_valid){
				$this->_message = 'Mã bảo vệ không chính xác';
				return false;
			}
			// validate username
			$res = $this->db->get_where('customer',array('username'=>$this->_username));	
			if($res->num_rows()){
				$this->_message = 'Tên đăng nhập này đã được sử dụng';
				return false;
			}
			// validate email
			$res = $this->db->get_where('customer',array('email'=>$this->_email));	
			if($res->num_rows()){
				$this->_message = 'Địa chỉ email này đã được sử dụng';
				return false;
			}
			// insert new customer			
			$this->db->set('username',$this->_username);
			$this->db->set('password',md5($this->_password));
			$this->db->set('fullname',$this->_fullname);
			$this->db->set('email',$this->_email);
			$this->db->set('phone',$this->_phone);
			$this->db->set('address',$this->_address);
			$this->db->set('website',$this->_website);
			$this->db->set('status',0);	//0: chua kich hoat, 1: da kich hoat, 2: tam khoa
			$this->db->insert('customer');
			// update time expire
			$this->db->where('username',$this->_username);
			$this->db->update('customer',array('time_expire'=>time()));
			set_cookie('customer_username',$this->_username,0,'','/','');
			// send active email
			$this->_goi_mail_kich_hoat();			
			return true;
    	}		
		return false;
	}
	
	function dang_nhap(){
		if(FALSE!==$this->input->post('hidden_status')){
			$this->_username 	= $this->input->post('txt_username');
			$this->_password 	= $this->input->post('txt_password');			
			$res = $this->db->get_where('customer',array('username'=>$this->_username,'password'=>md5($this->_password)));
			if($res->num_rows()){
				$this->db->where('username',$this->_username);
				$this->db->update('customer',array('time_expire'=>time()));
				set_cookie('customer_username',$this->_username,0,'','/','');
				return true;
			}
			else{
				$this->_message = 'Tên đăng nhập hoặc mật khẩu không đúng';
				return false;
			}			
		}
		return false;
	}
	
	function dang_xuat(){
		delete_cookie("customer_username");
	}
	
	function thong_tin(){
		$info['username'] 	= $this->_username;
		$info['fullname'] 	= $this->_fullname;
		$info['email']	 	= $this->_email;
		$info['phone'] 		= $this->_phone;
		$info['address'] 	= $this->_address;
		$info['website'] 	= $this->_website;		
		$info['message'] 	= $this->_message;
		return $info;
	}
	
	function _goi_mail_kich_hoat(){
		
	}	
	
	function kiem_tra_dang_nhap(){
		if(FALSE===get_cookie('customer_username')){	
			redirect('thanh_vien/dang_nhap','location');				
			return;		
		}
		$query = $this->db->get_where('customer',array('username'=>get_cookie('customer_username')));
		$time_exp = $query->row();
		if(get_cookie('customer_username')=='' || time()-$time_exp->time_expire>1800){
			delete_cookie("customer_username");
			redirect('thanh_vien/dang_nhap','location');
			return;
		}
		else{
			$this->db->where('username',get_cookie('customer_username'));
			$this->db->update('customer',array('time_expire'=>time()));
		}		
	}
	
	function thong_tin_tai_khoan(){
		if($this->_editting) return $this->thong_tin();
		$query = $this->db->get_where('customer',array('username'=>get_cookie('customer_username')));
		$info = $query->row();
		$this->_username	= $info->username;
		$this->_fullname	= $info->fullname;
		$this->_email		= $info->email;
		$this->_phone		= $info->phone;
		$this->_address		= $info->address;
		$this->_website		= $info->website;		
		return $this->thong_tin();
	}
	
	function cap_nhap_thong_tin_tai_khoan(){
		if(FALSE!==$this->input->post('hidden_status')){			
			$this->_fullname 	= $this->input->post('txt_fullname');
			$this->_email 		= $this->input->post('txt_email');
			$this->_phone 		= $this->input->post('txt_phone');
			$this->_address 	= $this->input->post('txt_address');
			$this->_website 	= $this->input->post('txt_website');			
			// validate email
			$res = $this->db->get_where('customer',array('username !='=>get_cookie('customer_username'),'email'=>$this->_email));	
			if($res->num_rows()){
				$this->_message = 'Địa chỉ email này đã được sử dụng';
				$this->_editting = true;
				return false;
			}
			// update customer info			
			$this->db->set('fullname',$this->_fullname);
			$this->db->set('email',$this->_email);
			$this->db->set('phone',$this->_phone);
			$this->db->set('address',$this->_address);
			$this->db->set('website',$this->_website);	
			$this->db->where('username',get_cookie('customer_username'));		
			$this->db->update('customer');
			return true;
		}
		return false;
	}
	
	function doi_mat_khau(){
		if(FALSE!==$this->input->post('hidden_status')){			
			$pwd_old 	= $this->input->post('txt_password_old');
			$pwd_new 	= $this->input->post('txt_password_new');
			// validate old password
			$res = $this->db->get_where('customer',array('username'=>get_cookie('customer_username'),'password'=>md5($pwd_old)));	
			if(!$res->num_rows()){
				$this->_message = 'Mật khẩu cũ không chính xác. Vui lòng nhập lại.';				
				return false;
			}
			// update new password
			$this->db->set('password',md5($pwd_new));
			$this->db->where('username',get_cookie('customer_username'));		
			$this->db->update('customer');
			return true;
		}
		return false;
	}
	
	function get_customer_id(){
		$query = $this->db->get_where('customer',array('username'=>get_cookie('customer_username')));
		return $query->row()->id;		
	}
	// add by phucduong
	// 
	function gui_email_doi_mat_khau()
	{
		if($this->input->post('txt_email')!='') {
			$query = $this->db->get_where('customer',array('email'=>$this->input->post('txt_email')));
			if(count($query->result())>0)
			{
				$str_pwd = md5(md5($this->input->post('txt_email').time('now')));
				$arr_update = array('string_reset_password'=>$str_pwd,'date_reset_password'=>date('Y-m-d H:i:s',time('now')),'time_expire'=>43200);
				$this->db->update('customer',$arr_update,array('email'=>$this->input->post('txt_email')));
				// send email
				$content ='';
				$this->load->library('email');
				$config['mailtype'] = 'html';
				$this->email->initialize($config);
				$this->email->from('info@phucthinhreal.com', 'Ban quản trị Website Phucthinhreal.com');
				$this->email->to($this->input->post('txt_email'));		
				$this->email->subject('Khôi phục mật khẩu- Phucthinhreal.com');
				$content .= 'Click vào <a href="'.base_url().'thanh_vien/khoi_phuc_mat_khau/'.$str_pwd.'"> đây </a> để đổi mật khẩu';
				$content .= ' hoặc copy link sau dán vào trình duyệt ';
				$content .=  base_url().'thanh_vien/khoi_phuc_mat_khau/'.$str_pwd;
				$content .= '<hr/>Ban quản trị website Phucthinhreal.com';
				$this->email->message($content);
				if ( ! $this->email->send())
				{
					$this->_message ='Gửi email có lỗi, vui lòng gửi lại !';
				}
				else
				$this->_message ='Gửi thành công, vui lòng kiểm tra email để lấy mật khẩu';
			}
			else 
			$this->_message = 'Email này không tồn tại!';
		}
	}
	function khoi_phuc_mat_khau($str_reset=''){
		if($str_reset!='') {
			delete_cookie('str_reset');
			set_cookie('str_reset',$str_reset,0,'','/','');		
		}
		else
		{
		 	$str_reset = get_cookie('str_reset');
		}
		$query = $this->db->get_where('customer',array('string_reset_password'=>trim($str_reset)));
		if(count($query->result())>0) {
			$temp = $query->result();
			$timenow = time('now');
			if($timenow - (strtotime($temp[0]->date_reset_password)+ $temp[0]->time_expire) < 0)
			{
				if(FALSE!==$this->input->post('txt_password_new')){								
					$pwd_new 	= $this->input->post('txt_password_new');					
					// update new password
					$this->db->set('password',md5($pwd_new));
					$this->db->where('string_reset_password',$str_reset);		
					$this->db->update('customer');
					delete_cookie('str_reset');	
					redirect('thanh_vien/dang_nhap','location');
				}
				return false;
			} 
			else 
			{
				$this->_message = 'Hết thời hạn đổi mật khẩu1';
			}
		}
	}
}
/* End of file */